package com.lihaozhe.security.filter;

import com.alibaba.fastjson.JSONObject;
import com.lihaozhe.util.string.StringUtil;
import lombok.SneakyThrows;
import org.springframework.security.authentication.AuthenticationServiceException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.util.StreamUtils;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.nio.charset.Charset;

/**
 * @author 李昊哲
 * @version 1.0.0
 * 2021/12/17 10:01
 */
public class JsonAuthenticationFilter extends UsernamePasswordAuthenticationFilter {
    @SneakyThrows
    @Override
    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
        if (!request.getMethod().equalsIgnoreCase("POST")) {
            throw new AuthenticationServiceException("Authentication method not supported: " + request.getMethod());
        } else {
            String contentType = request.getContentType();
            // 获取页面传递过来的验证码
            if (!StringUtil.isBlank(contentType) && contentType.contains("application/json")) {
                System.out.println(contentType);
                // 如果是json请求使用取参数逻辑
                String text = StreamUtils.copyToString(request.getInputStream(), Charset.forName("UTF-8"));
                if (StringUtils.hasText(text)) {
                    JSONObject jsonObj = JSONObject.parseObject(text);
                    System.out.println(JSONObject.toJSONString(jsonObj));
                    String username = jsonObj.getString("account");
                    username = username != null ? username : "";
                    username = username.trim();
                    String password = jsonObj.getString("authString");
                    password = password != null ? password : "";
                    UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(username, password);
                    this.setDetails(request, authRequest);
                    return this.getAuthenticationManager().authenticate(authRequest);
                }
            }
            System.out.println(contentType);
            // 如果不是json使用自带的过滤器获取参数
            String username = request.getParameter("account");
            username = username != null ? username : "";
            username = username.trim();
            String password = request.getParameter("authString");
            password = password != null ? password : "";
            UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(username, password);
            this.setDetails(request, authRequest);
            return this.getAuthenticationManager().authenticate(authRequest);
        }
    }
}